<template>
  <div class="record-detail" v-loading="loading">
    <ComNav :list="['客户信息', '客户信息详情']" v-if="!isAddOrEdit" />

    <div class="brand-title" v-if="!isAddOrEdit">
      <el-image :src="getLogo(customerDetail.logo)" style="width: 64px; height: 64px" fit="contain" />
      <div class="title-box">
        <p class="info-company">{{ customerDetail.zhName }}</p>
        <el-popover placement="bottom-start" width="auto" trigger="hover">
          <template #reference>
            <el-tag size="small">曾用名 {{customerDetail.originalNameVoList && customerDetail.originalNameVoList.length}}</el-tag>
          </template>
          <template v-if="customerDetail.originalNameVoList && customerDetail.originalNameVoList.length">
            <div v-for="(item, index) of customerDetail.originalNameVoList" :key="index">{{item.name}} -- {{item.changeDate?.slice(0, 10)}}</div>
          </template>
        </el-popover>
        <el-popover placement="bottom-start" width="auto" trigger="hover">
          <template #reference>
            <el-tag size="small" type="info" style="margin-left: 20px">注销吊销信息</el-tag>
          </template>
          <template v-if="customerDetail.revokeInfoVo">
            <div>
              <p>注销理由： {{customerDetail.revokeInfoVo.cancelReason}}</p>
              <p>注销时间： {{customerDetail.revokeInfoVo.cancelDate?.slice(0, 10)}}</p>
              <p>吊销理由： {{customerDetail.revokeInfoVo.revokeReason}}</p>
              <p>吊销时间： {{customerDetail.revokeInfoVo.revokeDate?.slice(0, 10)}}</p>
            </div>
          </template>
        </el-popover>
      </div>
      <div class="edit-btn" @click="toEdit">编辑</div>
      <div class="history-btn">
        <el-button size="default" @click="toHistory">客户历史信息记录</el-button>
      </div>
    </div>

    <div :class="['record-summary', isAddOrEdit ? 'main' : '']">
      <el-scrollbar>
        <div class="summary-main" v-if="!isAddOrEdit">
          <el-card shadow="hover">
            <ul class="brand-content">
              <li>客户类型：{{getCustomType(customerDetail.customType)}}</li>
              <li>是否上市：{{getIsOnStock(customerDetail.isOnStock)}}</li>
              <li>上市类型：{{customerDetail.stockType}}</li>
              <li>企业性质：{{getEntType(customerDetail.entType)}}</li>
              <li>展商类型：{{getCompanyType(customerDetail.companyType)}}</li>
              <li>更新日期：{{customerDetail.updatedDate}}</li>
              <li>企查查同步时间：{{customerDetail.qccSynDate}}</li>
              <li>cbe同步时间：{{customerDetail.cbeSynDate}}</li>
            </ul>
            <div class="brand-intro">
              <span :class="['text', isExpand ? 'expand' : '']"
                >简介：
                <div v-html="customerDetail.briefIntro" class="html-box"></div
              ></span>
              <span class="btn" @click="isExpand = !isExpand">{{isExpand ? '收起' : '展开'}}</span>
            </div>
          </el-card>

          <el-card shadow="hover" style="margin-top: 10px">
            <el-descriptions title="工商信息" :column="2" border>
              <el-descriptions-item label="统一社会信用代码" min-width="150px">{{customerDetail.creditCode}}</el-descriptions-item>
              <el-descriptions-item label="企业名称" min-width="150px">{{customerDetail.zhName}}</el-descriptions-item>
              <el-descriptions-item label="法定代表人" min-width="150px">{{customerDetail.operName}}</el-descriptions-item>
              <el-descriptions-item label="登记状态" min-width="150px">{{customerDetail.status}}</el-descriptions-item>
              <el-descriptions-item label="成立日期" min-width="150px">{{customerDetail.startDate}}</el-descriptions-item>
              <el-descriptions-item label="核准日期" min-width="150px">{{customerDetail.checkDate}}</el-descriptions-item>
              <el-descriptions-item label="注册资本" min-width="150px">{{customerDetail.registCapi}}</el-descriptions-item>
              <el-descriptions-item label="实缴资本" min-width="150px">{{customerDetail.recCap}}</el-descriptions-item>
              <el-descriptions-item label="股票代码" min-width="150px">{{customerDetail.stockNumber}}</el-descriptions-item>
              <el-descriptions-item label="组织机构代码" min-width="150px">{{customerDetail.orgNo}}</el-descriptions-item>
              <el-descriptions-item label="工商注册号" min-width="150px">{{customerDetail.no}}</el-descriptions-item>
              <el-descriptions-item label="企业类型" min-width="150px">{{customerDetail.econKind}}</el-descriptions-item>
              <el-descriptions-item label="营业期限" min-width="150px">
                <span>{{customerDetail.termStart}}</span>
                <span v-if="customerDetail.termStart && customerDetail.termEnd">至</span>
                <span>{{customerDetail.termEnd}}</span>
              </el-descriptions-item>
              <el-descriptions-item label="登记机关" min-width="150px">{{customerDetail.belongOrg}}</el-descriptions-item>
              <el-descriptions-item label="英文名" min-width="150px">{{customerDetail.enName}}</el-descriptions-item>
              <el-descriptions-item label="注册地址" min-width="150px">{{customerDetail.address}}</el-descriptions-item>
              <el-descriptions-item label="经营范围" min-width="150px">{{customerDetail.scope}}</el-descriptions-item>
            </el-descriptions>
          </el-card>

          <el-card shadow="hover" style="margin-top: 10px">
            <el-descriptions title="行政区域" border>
              <el-descriptions-item label="省份" width="60px">{{customerDetail.customAreaVo?.province}}</el-descriptions-item>
              <el-descriptions-item label="城市" width="60px">{{customerDetail.customAreaVo?.city}}</el-descriptions-item>
              <el-descriptions-item label="区域" width="60px">{{customerDetail.customAreaVo?.country}}</el-descriptions-item>
            </el-descriptions>
          </el-card>
        </div>

        <div v-if="!isAddOrEdit">
          <div class="visit-tab">
            <div
              v-for="(item ,index) of tabList"
              :key="index"
              :class="['tab-item', flag === item.label ? 'active' : '']"
              @click.stop="tabChange(item.label)"
            >
              <img :src="flag === item.label ? item.active : item.src" alt="" />
              <span>{{ item.label }}</span>
            </div>
          </div>

          <div class="info-main">
            <BrandDispaly
              :brandInfoVoList="customerDetail.brandInfoVoList"
              :companyType="customerDetail.companyType"
              ref="brandCompRef"
              v-if="['1', '2'].includes(customerDetail.companyType)"
            />
            <CloudInfo ref="cloudRef" v-else />
            <QuestionInfo ref="questionRef" :contentVoList="customerDetail.contentVoList" />
            <RecordIndex ref="recordRef" @recordInfo="recordInfo" />
          </div>
        </div>

        <AddAndEdit :id="customerDetail.id" :detail="customerDetail" v-else @back="back" @update="updateCustomer" />
      </el-scrollbar>
    </div>
  </div>
</template>

<script setup lang="ts">
import {defineComponent, onMounted, ref} from "vue";
import {useRoute, useRouter} from "vue-router";
import ComNav from '../../components/comNav/index.vue'
import ppzs from './assets/ppzs.png'
import ppzsActive from './assets/ppzs-active.png'
import xinxi from './assets/xinxi.png'
import xinxiActive from './assets/xinxi-active.png'
import wenjuan from './assets/wenjuan.png'
import wenjuanActive from './assets/wenjuan-active.png'
import jilu from './assets/jilu.png'
import jiluActive from './assets/jilu-active.png'
import BrandDispaly from './brandDisplay/index.vue'
import CloudInfo from './cloudInfo/index.vue'
import QuestionInfo from './questionInfo/index.vue'
import RecordIndex from './record/index.vue'
import { customDetail } from "@/api/customerInfo";
import AddAndEdit from "./addAndEdit.vue";
import {customerDetail} from "./customerService";
import {getLogo} from "@/utils";

const router = useRouter();
const route = useRoute();
const flag = ref('');
const loading = ref(false)
const isAddOrEdit = ref(false)
const tabList = ref<any[]>([]);
const brandCompRef = ref()
const cloudRef = ref()
const questionRef = ref<any>()
const recordRef = ref()
const recordDetail = ref<Record<string, any>>({
  visitDate: '',
  visitor: '',
  visitees: ''
})
const isExpand = ref(false)
const {proxy} = getCurrentInstance() as ComponentInternalInstance;
const {custom_type} = toRefs<any>(proxy?.useDict("custom_type"));
const {company_type} = toRefs<any>(proxy?.useDict("company_type"));
const {cusotm_stock} = toRefs<any>(proxy?.useDict("cusotm_stock"));
const {ent_type} = toRefs<any>(proxy?.useDict("ent_type"));

function getCustomType(type: string) {
  const item = custom_type.value.find((item: any) => item.value === type)
  return item?.label;
}

function getCompanyType(type: string) {
  const item = company_type.value.find((item: any) => item.value === type)
  return item?.label;
}

function getIsOnStock(type: string) {
  const item = cusotm_stock.value.find((item: any) => item.value === type)
  return item?.label;
}

function getEntType(type: string) {
  const item = ent_type.value.find((item: any) => item.value === type)
  return item?.label;
}

function tabChange(val: string) {
  flag.value = val;
  if (flag.value === '品牌展示')  brandCompRef.value?.brandBoxRef.scrollIntoView({ block: 'start', behavior: 'smooth' });
  if (flag.value === '展商信息')  cloudRef.value?.cloudBoxRef.scrollIntoView({ block: 'start', behavior: 'smooth' });
  if (flag.value === '问卷信息')  questionRef.value?.questionBoxRef.scrollIntoView({ block: 'start', behavior: 'smooth' });
  if (flag.value === '拜访记录')  recordRef.value?.recordBoxRef.scrollIntoView({ block: 'start', behavior: 'smooth' });
}

function toEdit() {
  isAddOrEdit.value = true
}

function back() {
  isAddOrEdit.value = false
}

function toHistory() {
  router.push({
    name: "customerHistoryList",
    query: { customId: customerDetail.value.id }
  });
}

function updateCustomer() {
  isAddOrEdit.value = false
  getCustomDetail(route.query.id as string)
}

function recordInfo(data: any[]) {
  recordDetail.value = data[0]?.info[0]
}

function getCustomDetail(id: string) {
  loading.value = true
  customDetail(id).then((res) => {
    customerDetail.value = res.data;
    tabList.value = [
      {
        label: '品牌展示',
        src: ppzs,
        active: ppzsActive,
        show: ['1', '2'].includes(customerDetail.value.companyType)
      },
      {
        label: '展商信息',
        src: xinxi,
        active: xinxiActive,
        show: customerDetail.value.companyType === '3'
      },
      {
        label: '问卷信息',
        src: wenjuan,
        active: wenjuanActive,
        show: true
      },
      {
        label: '拜访记录',
        src: jilu,
        active: jiluActive,
        show: true
      }
    ].filter((item) => item.show);
    flag.value = tabList.value[0].label;
  }).finally(() => loading.value = false)
}

onMounted(() => {
  customerDetail.value = {};
  if (route.query.id) {
    getCustomDetail(route.query.id as string)
  } else {
    isAddOrEdit.value = true
  }
})

defineComponent({name: 'CustomerInformation'})
</script>

<style scoped lang="scss">
.record-detail {
  width: 100%;
  height: 100%;
  padding: 20px 15px;
  background-color: #fff;

  .brand-title {
    position: relative;
    display: flex;
    align-items: center;
    margin: 30px 0 10px;

    .title-box {
      width: 100%;
      margin-left: 10px;

      .info-company {
        width: calc(100% - 120px);
        font-size: 28px;
        font-weight: 700;
        color: #2E3244;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }

    .edit-btn {
      position: absolute;
      top: -14px;
      right: 10px;
      width: 112px;
      height: 46px;
      padding-left: 50px;
      color: #FF4350;
      line-height: 44px;
      font-size: 16px;
      border: 1px solid #FF4350;
      border-radius: 10px;
      cursor: pointer;

      &::before {
        content: '';
        position: absolute;
        top: 50%;
        left: 24px;
        transform: translateY(-50%);
        display: inline-block;
        width: 16px;
        height: 16px;
        background: url('@/assets/edit.png');
      }
    }

    .history-btn {
      position: absolute;
      top: 40px;
      right: 10px;
    }
  }


  .record-summary {
    width: 100%;
    height: calc(100% - 114px);
    background-color: #fff;

    &.main {
      height: 100%;
    }

    .summary-main {
      position: relative;
      margin-top: 28px;
      padding: 0 20px;



      .brand-content {
        display: flex;
        flex-wrap: wrap;

        li {
          width: 50%;
          margin-bottom: 20px;
        }
      }

      .brand-intro {
        display: flex;
        align-items: flex-end;

        .text {
          width: calc(100% - 40px);
          word-break: break-all;
          text-overflow: ellipsis;
          overflow: hidden;
          white-space: nowrap;
          line-height: 26px;

          &.expand {
            white-space: normal;
          }
        }

        .btn {
          color: #128bed;
          cursor: pointer;
        }

      }
    }
  }

  .visit-tab {
    display: flex;

    .tab-item {
      display: flex;
      justify-content: center;
      width: 160px;
      height: 50px;
      margin: 30px 0 20px 0;
      padding-top: 8px;
      cursor: pointer;

      img {
        width: 22px;
        height: 22px;
        margin-right: 10px;
      }

      &.active {
        color: #fff;
        background: url('./assets/tab-active.png') no-repeat center;
      }
    }
  }

  .info-main {
    width: 100%;
  }
}
</style>
<style>
.html-box {
  width: auto!important;
  p, font {
    width: auto!important;
  }
}
</style>
